package com.house.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Package: com.wzy.controller
 * @Author: Clarence1
 * @Date: 2019/10/4 20:17
 */

@CrossOrigin
@RestController
public class UploadFileController {
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Result upload(@RequestParam("filename")MultipartFile fileUpload) {
        //获取文件名
        String fileName = fileUpload.getOriginalFilename();
        //获取文件后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //重新生成文件名
        fileName = UUID.randomUUID() + suffixName;
        //指定本地文件夹存储图片
        String filePath = "C:\\Users\\Administrator\\Desktop\\nginx-1.19.8\\nginxserver\\image\\";

        try {
            //将图片保存到static文件夹里
            fileUpload.transferTo(new File(filePath + fileName));
            return new Result(true,fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"上传失败");
        }
    }
}
